﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OperatorsGeneric
{
    class ComplexNumber
    {
        private double realPart;
        private double imagenaryPart;

        public ComplexNumber() : this(0, 0) { }

        public ComplexNumber(int realPart, int imagenaryPart)
        {
            this.realPart = realPart;
            this.imagenaryPart = imagenaryPart;
        }

        public double RealPart
        {
            get
            {
                return realPart;
            }
            set
            {
                realPart = value;
            }
        }

        public double ImagenaryPart
        {
            get
            {
                return imagenaryPart;
            }
            set
            {
                imagenaryPart = value;
            }
        }

        public static ComplexNumber operator + (ComplexNumber first , ComplexNumber second)
        {
            ComplexNumber result = new ComplexNumber();
            result.realPart = first.realPart+second.realPart;
            result.imagenaryPart = first.imagenaryPart+second.imagenaryPart;
            return result;
        }

        public static ComplexNumber operator - (ComplexNumber first , ComplexNumber second)
        {
            ComplexNumber result = new ComplexNumber();
            result.realPart = first.realPart-second.realPart;
            result.imagenaryPart = first.imagenaryPart-second.imagenaryPart;
            return result;
        }

        public static ComplexNumber operator * (ComplexNumber first , ComplexNumber second)
        {
            ComplexNumber result = new ComplexNumber();
            result.realPart = (first.realPart*second.realPart) - (first.imagenaryPart*second.imagenaryPart) ;
            result.imagenaryPart = (first.imagenaryPart*second.realPart) + (first.realPart*second.imagenaryPart) ;
            return result;
        }

        public static ComplexNumber operator / (ComplexNumber first , ComplexNumber second)
        {
            double a= first.realPart;
            double b = first.imagenaryPart;
            double c = second.realPart;
            double d = second.imagenaryPart;
            ComplexNumber result = new ComplexNumber();
            result.realPart = ((a*c)+(b*d))/((c*c)+(d*d));
            result.imagenaryPart =((b*c)-(a*d))/((c*c)+(d*d));
            return result;
        }

        public override string  ToString()
        {
 	        string result = string.Format("{0} + i.{1}",this.realPart,this.imagenaryPart);
            return result;
        }

        //public static implicit operator double(ComplexNumber complex) 
        //{
        //    return complex.realPart;
        //}

        public static explicit operator double(ComplexNumber complex)
        {
            return complex.realPart;
        }

        public double this[int index] 
        {
            get 
            {
                if (index == 0 || index == 1) 
                {
                    if (index == 0)
                    {
                        return realPart;
                    }

                    else 
                    {
                        return imagenaryPart;
                    }
                }

                else
                {
                    throw new IndexOutOfRangeException(
                    String.Format("Index {0} is invalid!", index));
                }
            }
        }
    }
}
